package com.prs.crm.action.oa;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.oa.Regulation;
import com.prs.crm.domain.oa.RegulationType;
import com.prs.crm.service.oa.RegulationService;
import com.prs.crm.service.oa.RegulationTypeService;

public class RegulationAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Regulation regulation;
	private RegulationService regulationService;
	private PaginationSupport pagination;
	private Integer[] regulationIds;
	private String currentDate;

	private File upload;
	private String uploadFileName;
	private String reg;

	private List<RegulationType> types;

	private RegulationTypeService regulationTypeService;

	public List<RegulationType> getTypes() {
		return types;
	}

	public void setTypes(List<RegulationType> types) {
		this.types = types;
	}

	public RegulationTypeService getRegulationTypeService() {
		return regulationTypeService;
	}

	public void setRegulationTypeService(
			RegulationTypeService regulationTypeService) {
		this.regulationTypeService = regulationTypeService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	@Action("listRegulation")
	public String execute() {
		return SUCCESS;
	}

	@Action("listRegulationByReg")
	public String listRegulationByReg() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Regulation.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (reg != null) {
			if (!"all".equals(reg)) {
				reg = reg.split(",")[0];
				if (ToolKits.isInteger(reg)) {
					criteria.createAlias("type", "ty");
					criteria.add(Restrictions
							.eq("ty.id", Integer.parseInt(reg)));
				}
			}
		}
		this.setPagination(this.getRegulationService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editRegulation")
	public String edit() {
		this.setCurrentDate(ToolKits.currentDate());
		this.types = this.getRegulationTypeService().getAllRegulationType();
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveRegulation", results = {
			@Result(name = "success", type = "redirect", location = "listRegulation"),
			@Result(name = "none", type = "redirect", location = "alterRegulation"),
			@Result(name = "error", type = "chain", location = "editRegulation") })
	public String save() {
		if (regulation.getTitle() == null
				|| "".equals(regulation.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (regulation.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (regulation.getCreattime() == null) {
			addActionError("登记时间不能为空");
			return ERROR;
		}
		if (regulation.getDept() == null) {
			addActionError("部门不能为空");
			return ERROR;
		}

		if (regulation.getId() == null) {
			this.setPrompt("规章制度提交成功!");
			regulation.setState(Constrains.TASK_AUDITING);
			this.getRegulationService().saveRegulation(regulation, upload,
					uploadFileName);
			return SUCCESS;
		} else {
			this.setPrompt("修改规章制度成功!");
			regulation.setState(Constrains.TASK_AUDITING);
			this.getRegulationService().saveRegulation(regulation, upload,
					uploadFileName);
			return NONE;
		}
	}

	@Action(value = "removeRegulation", results = {
			@Result(name = "success", type = "redirect", location = "alterRegulation"),
			@Result(name = "error", type = "chain", location = "alterRegulation") })
	public String remove() {
		if (regulationIds == null) {
			this.addActionError("请选择要删除的规章制度");
			return ERROR;
		}
		this.getRegulationService().batchRemove(regulationIds);
		return SUCCESS;
	}

	@Action("alterRegulation")
	public String alterRegulation() {
		return SUCCESS;
	}

	@Action("alterRegulationByReg")
	public String alterRegulationByReg() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Regulation.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		if (reg != null) {
			if (!"all".equals(reg)) {
				reg = reg.split(",")[0];
				if (ToolKits.isInteger(reg)) {
					criteria.createAlias("type", "ty");
					criteria.add(Restrictions
							.eq("ty.id", Integer.parseInt(reg)));
				}
			}
		}
		this.setPagination(this.getRegulationService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewRegulation")
	public String viewRegulation() {
		return SUCCESS;
	}

	public Regulation getRegulation() {
		return regulation;
	}

	public void setRegulation(Regulation regulation) {
		this.regulation = regulation;
	}

	public RegulationService getRegulationService() {
		return regulationService;
	}

	public void setRegulationService(RegulationService regulationService) {
		this.regulationService = regulationService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getRegulationIds() {
		return regulationIds;
	}

	public void setRegulationIds(Integer[] regulationIds) {
		this.regulationIds = regulationIds;
	}

	public void setCurrentDate(String currentDate) {
		this.currentDate = currentDate;
	}

	public String getCurrentDate() {
		return currentDate;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public String getReg() {
		return reg;
	}

	public void setReg(String reg) {
		this.reg = reg;
	}

}
